<template>
    <div class="box">
        <el-row>
            <el-col :span="22" :push="1">
                <div class="grid-content bg-purple">
                    <div class="kuang"></div>
                    <div class="box_text">
                        {{title}}
                        <el-divider></el-divider>
                    </div>
                    <div style="margin-top: 20px; margin-left: 40px; width: 500px">
                        <el-form label-position="right" label-width="100px">
                            <el-form-item label="投保车辆">
                                <el-select v-model="form.insuranceNumber">
                                    <el-option v-for="item in carNumbers" :key="item.id" :label="item.carNumber"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="投保时间">
                                <el-date-picker v-model="form.insuranceTime" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd"
                                    placeholder="投保时间">
                                </el-date-picker>
                            </el-form-item>
                            <el-form-item label="投保单位">
                                <el-select v-model="form.insuranceUnit" placeholder="投保项目">
                                    <span v-for="(item, index) in carStates" :key="index">
                                        <el-option :label="item.label" :value="item.value"></el-option>
                                    </span>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="商业险金额">
                                <el-input-number v-model="form.insuranceOne" @blur="sum" controls-position="right"></el-input-number>元
                            </el-form-item>
                            <el-form-item label="交强险金额">
                                <el-input-number v-model="form.insuranceTwo" @blur="sum" controls-position="right"></el-input-number>元
                            </el-form-item>
                            <el-form-item label="车船险金额">
                                <el-input-number v-model="form.insuranceThree" @blur="sum" controls-position="right"></el-input-number>元
                            </el-form-item>
                            <el-form-item label="其他金额">
                                <el-input-number v-model="form.insuranceFour" @blur="sum" controls-position="right"></el-input-number>元
                            </el-form-item>
                            <el-form-item label="费用合计">
                                <el-input-number v-model="form.insuranceSum" controls-position="right"></el-input-number>元
                            </el-form-item>
                            <el-form-item label="生效时间">
                                <el-date-picker v-model="form.insuranceStarttime" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd"
                                    placeholder="生效时间">
                                </el-date-picker>
                            </el-form-item>
                            <el-form-item label="到期时间">
                                <el-date-picker v-model="form.insuranceEndtime" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd"
                                    placeholder="到期时间">
                                </el-date-picker>
                            </el-form-item>
                        </el-form>
                    </div>
                    <div class="kuang"></div>
                    <div class="box_text">
                        更多信息
                        <el-divider></el-divider>
                    </div>
                    <div style="margin-top: 20px; margin-left: 40px; width: 500px">
                        <el-form label-position="right" label-width="100px">
                            <el-form-item label="备注">
                                <el-input type="textarea" v-model="form.insuranceRambak"
                                    :autosize="{ minRows: 2, maxRows: 4 }" placeholder="请输入内容">
                                </el-input>
                            </el-form-item>
                            <el-form-item label="投保凭证">
                                <el-upload class="avatar-uploader" action="http://localhost:8030/ftp/upload"
                                    :show-file-list="false" :on-success="handleAvatarSuccess"
                                    :before-upload="beforeAvatarUpload" :headers="upheaders">
                                    <img v-if="imageUrl" :src="form.insuranceImg" class="avatar" />
                                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    <div slot="tip" class="el-upload__tip">
                                        只能上传jpg/png文件，且不超过500kb
                                    </div>
                                </el-upload>
                            </el-form-item>
                        </el-form>
                        <el-button type="primary" @click="onSubmit">立即提交</el-button>
                        <el-button @click="back">取消</el-button>
                    </div>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import axios from "axios";
export default {
    data() {
        return {
            form: {
                createUser:JSON.parse(localStorage.getItem("user")).id
            },
            options: {},
            title:"添加投保信息",
            carStates: [
            { value: "0", label: "中国平安车险" },
            { value: "1", label: "中国人民保险" },
            { value: "2", label: "太平洋保险" },
            { value: "3", label: "中国人寿保险" },
        ],
            carNumbers: {},
            imageUrl: false,
            upheaders: {
                Authorization: "Bearer" + localStorage.getItem("token"),
            },
        };
    },
    created() {
        this.getCarNumber()
        if (this.$route.query.id != undefined) {
            this.title = "修改投保信息"
            this.echoCarcheer()
        }
    },
    methods: {
        sum(){
            if(this.form.insuranceOne !=0 && this.form.insuranceTwo !=0 && this.form.insuranceThree!=0&& this.form.insuranceFour!=0){
                this.form.insuranceSum = this.form.insuranceOne+this.form.insuranceTwo+this.form.insuranceThree+this.form.insuranceFour
            }
        },
        //回显
        echoCarcheer() {
            axios.post("carinsurance/echoinsurance?id=" + this.$route.query.id).then((res) => {
                this.form = res.data.data
                this.imageUrl = true
            })
        },
        //提交
        onSubmit() {
            if (this.$route.query.id == undefined) {
                axios.post("carinsurance/addinsurance", this.form).then((res) => {
                    console.log(res)
                    if (res.data.code === 200) {
                        this.$message.success(res.data.data)
                        this.form = {}
                        this.$router.go(-1)
                    }
                })
            } else {
                this.form.id = this.$route.query.id 
                axios.post("carinsurance/upinsurance", this.form).then((res) => {
                    console.log(res)
                    if (res.data.code === 200) {
                        this.$message.success(res.data.data)
                        this.form = {}
                        this.$router.go(-1)
                    }
                })
            }
        },
        //查询车牌号
        getCarNumber() {
            axios.get("/car/getCarNumber").then((res) => {
                console.log(res)
                this.carNumbers = res.data.data
            })
        },
        // 返回上一页
        back(){
            this.$router.go(-1)
        },
        handleAvatarSuccess(res, file) {
            console.log(res);
            if (res != "上传失败") {
                this.$message.success("上传成功");
                this.form.insuranceImg = res;
                this.imageUrl = true;
            } else {
                this.imageUrl = false;
                this.$message.error(res);
            }
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === "image/jpeg" || "image/png";
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error("上传头像图片只能是 JPG或者png 格式!");
            }
            if (!isLt2M) {
                this.$message.error("上传头像图片大小不能超过 500kb!");
            }
            return isJPG && isLt2M;
        },
    }
};
</script>

<style scoped>
/* element-ui样式 */
.el-row {
    margin-bottom: 10px;

    &:last-child {
        margin-bottom: 0;
    }
}

.el-col {
    border-radius: 4px;
}

.bg-purple {
    background: #fff;
    border-radius: 10px;
}

/* 自定义样式 */
/* * {
      margin: 0px;
      padding: 0px;
    } */
.box {
    width: 98%;
    height: 100%;
    margin: auto;
    /* background-color: red; */
}

.box_text {
    padding-top: 20px;
    /* padding-left: 40px; */
    margin-left: 40px;
    font-size: 13px;
}

/* 获取负责人start */
.avatar-uploader-icon[data-v-c59672b0] {
    border: 1px solid rgba(198, 201, 208, 1);
    background-color: rgba(248, 248, 248, 1);
    border-radius: 50%;
}

.avatar-uploader-icon {
    font-size: 20px;
    color: #8c939d;
    width: 50px;
    height: 50px;
    line-height: 50px;
    text-align: center;
}


.architecture {
    width: 271px;
    height: 39px;
    padding-left: 20px;
    border-radius: 5px;
}

.architecture:hover {
    background-color: #f8f8f8;
}

.architectureActe {
    width: 271px;
    height: 39px;
    line-height: 39px;
    padding-left: 20px;
    border-radius: 5px;
    background-color: rgba(231, 244, 255, 1);
    ;
}

/* 竖着的水平线 */
.myHr {
    border-left: 2px solid#989DAA;
    ;
    height: 100%;
    left: 48%;
    top: 0px;
    position: absolute;
}

.log {
    width: 27px;
    height: 27px;
    background-color: rgba(158, 208, 255, 1);
    float: left;
    position: relative;
    top: 15%;
    border-radius: 50%;
}

.log img {
    width: 13px;
    height: 13px;
    position: relative;
    bottom: 15%;
    left: 25%;
}

/* 获取负责人end */
.kuang {
    width: 4.5px;
    height: 25px;
    background-color: #409EFF;
    margin-left: 30px;
    position: relative;
    top: 43px;
}
</style>